package com.xiaotu.spider.maoyan.pageprocessor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.MaoYanMovieCityCinemaBoxModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.DateUtils;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.PropertiesUtil;
import com.xiaotu.common.util.RegexUtils;
import com.xiaotu.common.util.UUIDUtils;
import com.xiaotu.server.service.maoyan.MaoYanMovieBasicInfoService;
import com.xiaotu.spider.AUrlPageProcessor;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

/***
 * 城市票房
 * 影投票房
 */
@Component("MaoYanMovieCityCinemaBoxPageProcessor")
public class MaoYanMovieCityCinemaBoxPageProcessor extends AUrlPageProcessor {

	
	private Map<String,String> cookies = new HashMap<String,String>();
	
	{
		cookies.put("Cookie", PropertiesUtil.getProperty("BoxOffice.site.Cookie"));
	}
	
	public static final String START_URL = "http://piaofang.maoyan.com/dashboard";//初始url
	
	private String CITY_URL = "http://piaofang.maoyan.com/movie/%s/cityBox?date=%s";//分城票房

    private String SHADOW_URL = "http://piaofang.maoyan.com/movie/%s/shadowBox?date=%s";//影投票房
    
    private static String dateRegex = "^[0-9]{4}-[0-9]{2}-[0-9]{2}$";
    
    
	public static void main(String[] args) {
		String str = "2019-09-9";
		System.out.println(RegexUtils.regexMatch(dateRegex, str));
	}
	
    
    
    @Autowired
    private MaoYanMovieBasicInfoService maoYanMovieBasicInfoService;
    
	@Override
	public void process(Page page) {
		
		Map<String, Object> extrasMap = page.getRequest().getExtras();
		
		switch (this.getPageDataType(page))
        {
            case Constants.PageDataType.TV_CITY:
                this.cityProcess(page, StringUtils.EMPTY,extrasMap);
                break;
            case Constants.PageDataType.MOVIE_COMPANY:
                this.cityProcess(page, "/span",extrasMap);
                break;
            default:
            	this.addRequestProcess(page);
            	break;
        }
	}
	
	private void addRequestProcess(Page page) {
		
		//List<Map<String,Object>> list = maoYanMovieBasicInfoService.queryBoxMovieList();
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		
  		for(Map<String,Object> m : list){
  			
  			String dataid = m.get("dataid").toString();
  			String tvname = m.get("tvname").toString();
  			String maxdate = m.get("play_date").toString();
  			
  			Map<String,Object> paramMap1 = new HashMap<String,Object>();
  			paramMap1.put("dataid", dataid);
  			paramMap1.put("tvname", tvname);
  			paramMap1.put("maxdate", maxdate);
  			paramMap1.put("count", 1);
  			Map<String,Object> paramMap2 = new HashMap<String,Object>();
  			paramMap2.put("dataid", dataid);
  			paramMap2.put("tvname", tvname);
  			paramMap2.put("maxdate", maxdate);
  			paramMap2.put("count", 1);
  			
  			PageUtils.addTargetRequest(page, String.format(SHADOW_URL, dataid, maxdate), paramMap1);
  			PageUtils.addTargetRequest(page, String.format(CITY_URL, dataid, maxdate), paramMap2);
  			
  		}
	}

	/**
     * 分城数据爬取(因为影投数据的页面结构与分城相似，故写在一起)
     *
     * @param page
     * @param tag  为“/span”说明爬取的是影投数据
     */
    private void cityProcess(Page page, String tag ,Map<String, Object> extrasMap)
    {
    	//获取是否第一次请求
    	int flag = Integer.parseInt(extrasMap.get("count").toString());
    	if(flag == 1){
    		//当前页面的日期
        	String currPageDate = page.getHtml().xpath("//span[@class='m-calendar-querydate ']/@rawdate").get();
        	
        	if(null == currPageDate){
        		throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        	}
        	
        	//当前参数日期
        	String currParamDate = extrasMap.get("maxdate").toString();
        	
        	if(currPageDate.equals("unde-fi-ned")){
        		throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        	}
        	if(RegexUtils.regexMatch(dateRegex, currPageDate)){
        		if(!currPageDate.equals(currParamDate)){
        			//更新参数日期
        			extrasMap.put("maxdate", currPageDate);
        		}
        	}
    	}
    	
        List<String> areaNameList = page.getHtml().xpath
                ("//div[@class='t-main-col']/div[@class='t-row']/div[@class='t-col']" + tag +
                        "/text()").all();
        if (areaNameList == null || areaNameList.isEmpty()) {
        	//获取当前的连接地址
        	this.addOtherRequset(page, (StringUtils.isEmpty(tag) ? CITY_URL : SHADOW_URL), extrasMap.get("maxdate").toString(), extrasMap);
        	throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        }
        Html html = PageUtils.spiderAudienceRating(page,cookies);
        List<String> boxList = html.xpath("//div[@class='t-change-wrapper']//div[@class='t-row']")
                .all();
        if (boxList == null || boxList.isEmpty() || boxList.size() != areaNameList.size())
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        List<MaoYanMovieCityCinemaBoxModel> cityList = new ArrayList<MaoYanMovieCityCinemaBoxModel>();
        int count=0;
        for(int i = 0;i<areaNameList.size();i++)
        {
            List<String> valueList = new Html(boxList.get(i)).xpath("//i/text()").all();
            if (valueList.size() != 9)
                throw new SpiderException(SpiderExceptionCode.DataInvalideException);
            if(valueList.get(0).equals("--") || valueList.get(0).equals("0.0")) {
            	count++;
            }
            MaoYanMovieCityCinemaBoxModel maoYanMovieCityCinemaModel = new MaoYanMovieCityCinemaBoxModel(
            		extrasMap.get("maxdate").toString(),
            		UUIDUtils.getId(),
            		extrasMap.get("dataid").toString(),
            		extrasMap.get("tvname").toString(),
            		areaNameList.get(i),
                    valueList.get(0).replace("--", "") + "万",
                    valueList.get(1).replace("--", ""), 
                    valueList.get(2).replace("--", ""), 
                    valueList.get(3).replace("--", ""),
                    valueList.get(4).replace("--", ""),
                    valueList.get(5).replace("--", ""),
                    valueList.get(6).replace("--", ""),
                    valueList.get(7).replace("--", ""),
                    valueList.get(8).replace("--", ""));
            		cityList.add(maoYanMovieCityCinemaModel);
        };

        if(count==areaNameList.size()) {
        	this.addOtherRequset(page, (StringUtils.isEmpty(tag) ? CITY_URL : SHADOW_URL), extrasMap.get("maxdate").toString(), extrasMap);
        	throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        }
        page.putField(MaoYanMovieCityCinemaBoxModel.class.getName() + (StringUtils.isEmpty(tag) ? StringUtils
                .EMPTY : Constants.PageDataType.MOVIE_COMPANY), cityList);
    }
	
    
    public void addOtherRequset(Page page, String reqUrl, String currDate,Map<String, Object> extrasMap) {
    	String maxdate = DateUtils.getSpecifiedDayBefore(currDate,DateUtils.DAY_FORMAT);
    	int count = Integer.parseInt(extrasMap.get("count").toString());
    	//往前推30天
    	if(count<30){
    		count++;
    		extrasMap.put("maxdate", maxdate);
        	extrasMap.put("count", count);
        	PageUtils.addTargetRequest(page, String.format(reqUrl, extrasMap.get("dataid").toString(), maxdate), extrasMap);
        	System.out.println("添加的当前url="+String.format(reqUrl, extrasMap.get("dataid").toString(), maxdate));
    	}
    	
    }
    
    
	@Override
	protected String[] getStartRequestUrls() {
		return new String[]{START_URL};
	}

	@Override
	protected String getSpiderType() {
		return Constants.SpiderTaskType.MOVIE_BOXOFFICE;
	}

	@Override
	public String getPageDataType(Page page) {
		String url = page.getUrl().toString();
        if (url.contains("/cityBox"))
            return Constants.PageDataType.TV_CITY;
        if (url.contains("/shadowBox"))
            return Constants.PageDataType.MOVIE_COMPANY;
        return "addRequest";
	}
	
}
